package com.gitee.jjnnzb.designpatterninspring.business.login.handler;


import com.gitee.jjnnzb.designpatterninspring.business.login.entity.vo.TokenRt;

import java.util.Map;

/**
 * 登录模板抽象类
 *
 * @author Jjn
 * @since 2023/8/7 09:30
 */
public abstract class LoginTemplate {
    
    protected abstract void checkCaptcha(Map<String, Object> map);
    
    protected abstract void checkSystemPrivilege(Map<String, Object> map);
    
    protected abstract void checkLoginIp(Map<String, Object> map);
    
    protected abstract void checkUser(Map<String, Object> map);
    
    protected abstract void generateToken(Map<String, Object> map);
    
    protected abstract void saveLoginRecord(Map<String, Object> map);
    
    protected abstract void analyseRequestInfo(Map<String, Object> map);
    
    /**
     * 返回策略的适用key
     *
     * @return 策略的key
     */
    protected abstract String getVersion();
    
    public TokenRt login0(Map<String, Object> map) {
        // 格式化请求信息，如请求IP等
        analyseRequestInfo(map);
        // 校验图形验证码
        checkCaptcha(map);
        // 用户名及密码校验
        checkUser(map);
        // 校验用户是否具备指定权限的范围权限
        checkSystemPrivilege(map);
        // 校验登录IP
        checkLoginIp(map);
        // 按策略生成token
        generateToken(map);
        // 保存登录记录
        saveLoginRecord(map);
        // 返回处理结果
        return new TokenRt((String) map.get("token"));
    }
}
